// 路由模块的封装

// 工程化
import Vue from 'vue'
import VueRouter from 'vue-router'
Vue.use(VueRouter)
// 创建路由模块
const router = new VueRouter({
  routes: [{
      path: '/',
      redirect: {
        name: 'index'
      }
    },
    {
      name: 'index',
      path: '/index',
      component: () => import('@/views/index.vue')
    }, {
      name: 'login',
      path: '/login',
      component: () => import('@/views/user/login.vue')
    },
    {
      name: 'register',
      path: '/register',
      component: () => import('@/views/user/register.vue')
    },
    {
      name: 'personal',
      path: '/personal/:id',
      component: () => import('@/views/user/personal.vue')
    },
    {
      name: 'edit_profile',
      path: '/edit_profile/:id',
      component: () => import('@/views/user/edit_profile.vue')
    },
    {
      name: 'articleDetail',
      path: '/articleDetail/:id',
      component: () => import('@/views/post/articleDetail.vue')
    },
    {
      name: 'follow',
      path: '/follow',
      component: () => import('@/views/user/follow.vue')
    },
    {
      name: 'star',
      path: '/star',
      component: () => import('@/views/user/star.vue')
    },
    {
      name: 'comment',
      path: '/comment/:id',
      component: () => import('@/views/comment.vue')
    }
  ]
})


//  导航守卫
router.beforeEach((to, from, next) => {
  if (to.path.indexOf('/personal/') !== -1) {
    let token = localStorage.getItem('hmtt_token')
    if (token) {
      next()
    } else {
      next({
        name: 'login'
      })
    }

  } else {
    next()
  }

})
// 暴露
export default router